package com.example.framework.base;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


/**
 * @author:0.2℃
 * @date： 2022-03-30 22:59
 * @Description ：基类
 * -
 */
public class BaseActivity extends AppCompatActivity {

    // 权限数组
    String[] mStrPermissions = {
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    // 用来保存还没有申请到的权限
    private List<String> mListPermissions = new ArrayList<>();

    // 保存没有同意的失败权限
    private List<String> mListNoPermissions = new ArrayList<>();

    private int requestCode;
    private OnPermissionResult result;

    protected void request(int requestCode, OnPermissionResult result) {
        if (checkPermissionAll()) {
            requestPermissionAll(requestCode, result);
        }
    }

    /**
     * 权限检查
     * @param permission
     * @return
     */
    protected boolean checkPermission(String permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int checked = checkSelfPermission(permission);
            return checked == PackageManager.PERMISSION_GRANTED;
        }
        return false;
    }

    /**
     * 判断是否需要申请权限
     * @return
     * true : 需要
     * false : 不需要
     */
    protected boolean checkPermissionAll() {
        mListPermissions.clear();
        for (int i = 0; i < mStrPermissions.length; i++) {
            boolean checked = checkPermission(mStrPermissions[i]);
            if (!checked) {
                mListPermissions.add(mStrPermissions[i]);
            }
        }
        return mListPermissions.size() > 0 ? true : false;
    }

    /**
     * 权限请求
     * @param mPermissions
     * @param requestCode
     */
    protected void requestPermission(String[] mPermissions, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(mPermissions, requestCode);
        }
    }

    /**
     * 请求所有未申请到的权限
     * @param requestCode
     */
    protected void requestPermissionAll(int requestCode, OnPermissionResult result) {
        this.requestCode = requestCode;
        this.result = result;
        requestPermission((String[])mListPermissions.toArray(new String[mListPermissions.size()]), requestCode);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
        mListNoPermissions.clear();
        if (requestCode == requestCode) {
            if (grantResults.length > 0) {
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        // 没有申请到的权限
                        mListNoPermissions.add(permissions[i]);
                    }
                }

                if (result != null) {
                    if (mListNoPermissions.size() == 0) {
                        result.onSuccess();
                    } else {
                        result.onFailed(mListNoPermissions);
                    }
                }
            }
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 为了让外部知道申请权限的结果，定义一个接口
     */
    protected interface OnPermissionResult {
        void onSuccess();

        void onFailed(List<String> mListNoPer);
    }

    /**
     * 判断窗口权限
     * @return
     * true : 有
     * false:没有
     */
    protected boolean checkWindowPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.canDrawOverlays(this);
        }
        return false;
    }

    /**
     * 请求窗口权限
     * @param requestCode
     */
    protected void requestWindowPermissions(int requestCode) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, requestCode);
    }
}
